package tests;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Random;

import models.Hangman3;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import tools.DateHeader;
import tools.FileUtil;
import tools.SeedRNGCommandLine;
import tools.XmlParserUtility;

/**
 * 
 * @author J Paul Gibson<br>
 * 
 *         Random play of hangman game to illustrate functionality<br>
 *         Expected typical output:<br>
 * 
 *         <pre>
 *  The seed used for the random number generator in the test is 0.
 * You can override this value by passing an integer value as a main argument parameter, if you so wish.
 * 
 * 
 * *******************************************************************
 * Execution Date/Time 2011/11/07 17:50:57
 * *******************************************************************
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: yellow
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: yellow
 * Letters Found: ??????
 * Letters Tried: --cd---h-j---n-pqr---v-x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: -bc---g-----mn----st-vw-y-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: indigo
 * Letters Found: ??d?g?
 * Letters Tried: ---d--g--jk----p-rst-v-xyz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: o?ange
 * Letters Tried: abc-efg--j-l-nopq-s-u-w---
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: green
 * Letters Found: ?????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: green
 * Letters Found: ?r???
 * Letters Tried: a--d-f-h-j--m-op-r--u---y-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: indigo
 * Letters Found: ?n??g?
 * Letters Tried: -b--e-gh-j-lmn----s---wx-z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: o??n??
 * Letters Tried: -b-d----ijk--no----t-vwx-z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: red
 * Letters Found: r?d
 * Letters Tried: -bcd------k-m-opqrs-----yz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: green
 * Letters Found: ?????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: green
 * Letters Found: gr??n
 * Letters Tried: ab---fg----lmnopqr-t---x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: ?ra???
 * Letters Tried: a-c--f---jkl---pqrs--v-x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: blue
 * Letters Found: ????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: blue
 * Letters Found: ?l??
 * Letters Tried: --cd-fgh---l-nop-r----w---
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: ??a???
 * Letters Tried: a-c----h--klm---q-s---wxy-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: yellow
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: yellow
 * Letters Found: yell?w
 * Letters Tried: a-cde-g---klm-----st--wxyz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: red
 * Letters Found: re?
 * Letters Tried: ----e--h--klm-op-rs-uv-x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: violet
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: violet
 * Letters Found: vio?e?
 * Letters Tried: ab-de-ghij----opq---uv---z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: ?r??g?
 * Letters Tried: --c---ghij------qrs-u--xyz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: indigo
 * Letters Found: i??ig?
 * Letters Tried: ab--e-g-ij------qr--u-wx-z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: violet
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: violet
 * Letters Found: v?ole?
 * Letters Tried: --cdefg----l--opqr--uv-xy-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 5 from 10
 * Current Word: indigo
 * Letters Found: indigo
 * Letters Tried: a--d--g-i----no---s--v-x-z
 * 
 * ******Congratulations - you have finally won ********
 * </pre>
 * 
 */
public class Hangman3BRandomTest {

	public static void main(String[] args) {
		String randomEnglishWord = "";
		char letterBeingTried = ' ';
		HashMap<String, String> dict = new HashMap<String, String>();
		HashMap<String, Integer> dictCount = new HashMap<String, Integer>();

		Random rng = SeedRNGCommandLine.getRandom(args);
		System.out.println(DateHeader.dateString());

		XmlParserUtility xp = new XmlParserUtility();
		Document dom = xp.ReadXMLFile("dict.xml");
		ArrayList<Element> al = (ArrayList<Element>) xp.ParseXMLDocument(
				"word", dom);

		ListIterator<Element> litr = al.listIterator();
		while (litr.hasNext()) {
			Element el = litr.next();
			System.out.println("EngWord = " + xp.getTextValue(el, "en")
					+ ", FrenchWord = " + xp.getTextValue(el, "fr")
					+ ", Count = " + xp.getTextValue(el, "count"));
			dict.put(xp.getTextValue(el, "en"), xp.getTextValue(el, "fr"));
			try {
				dictCount.put(xp.getTextValue(el, "en"),
						Integer.parseInt(xp.getTextValue(el, "count").trim()));
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
				dictCount.put(xp.getTextValue(el, "en"), new Integer(0));
			}

		}

		Hangman3 hangmanEnglish = new Hangman3(rng, dict.keySet(), true);
		Hangman3 hangmanFrench = new Hangman3(rng, dict.values());
		int cnt = 0;
		while (!hangmanEnglish.foundWord() || !hangmanFrench.foundWord()) {

			System.out.println("\n*******Starting A New Game " + ++cnt
					+ " ********");
			// hangmanenglish.randomWord();
			randomEnglishWord = hangmanEnglish.randomWordStr();
			dictCount.put(randomEnglishWord,
					dictCount.get(randomEnglishWord) + 1);
			hangmanFrench.setRandomWordStr(dict.get(randomEnglishWord));
			System.out.println(hangmanEnglish);
			System.out.println("**FR**BELOW**");
			System.out.println(hangmanFrench);
			while (!hangmanEnglish.gameOver() && !hangmanFrench.gameOver()) {
				letterBeingTried = (char) ('a' + rng.nextInt(26));
				hangmanEnglish.tryLetter(letterBeingTried);
				hangmanFrench.tryLetter(letterBeingTried);
			}
			System.out.println(hangmanEnglish);
			System.out.println("**FR**BELOW**");
			System.out.println(hangmanFrench);
		}
		System.out
				.println("\n*******Congratulations - you have finally won ********");

		StringBuffer sb = new StringBuffer();
		sb.append("<words>");
		dict.keySet().iterator();

		Iterator<String> itr = dict.keySet().iterator();
		String key = "";
		while (itr.hasNext()) {
			key = itr.next();
			sb.append("<word><en>" + key + "</en><fr>" + dict.get(key)
					+ "</fr><count>" + dictCount.get(key) + "</count></word>");

		}

		sb.append("</words>");
		FileUtil.writeToFile(sb);
	}
}
